package main

import (
	"encoding/gob"
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"

	"honeybadgerbft/hbbft"
)

func main() {
	fmt.Println("Testing HoneyBadger network communication...")

	// 注册gob类型
	registerGobTypes()

    // 启动4个节点进行实验（n=4, f=1）
    nodes := []int{0, 1, 2, 3}
    processes := make([]*exec.Cmd, len(nodes))

    // 并行启动，统一peers
    peer0 := "1:localhost:8001,2:localhost:8002,3:localhost:8003"
    peer1 := "0:localhost:8000,2:localhost:8002,3:localhost:8003"
    peer2 := "0:localhost:8000,1:localhost:8001,3:localhost:8003"
    peer3 := "0:localhost:8000,1:localhost:8001,2:localhost:8002"

    cmd0 := exec.Command("./honeybadger",
        "-id", "0",
        "-addr", "localhost:8000",
        "-peers", peer0,
        "-n", "4",
        "-f", "1",
        "-batch", "64")
    cmd0.Stdout = os.Stdout
    cmd0.Stderr = os.Stderr
    if err := cmd0.Start(); err != nil {
        log.Fatalf("Failed to start node 0: %v", err)
    }
    processes[0] = cmd0
    fmt.Println("Started node 0 on localhost:8000")

    cmd1 := exec.Command("./honeybadger",
        "-id", "1",
        "-addr", "localhost:8001",
        "-peers", peer1,
        "-n", "4",
        "-f", "1",
        "-batch", "64")
    cmd1.Stdout = os.Stdout
    cmd1.Stderr = os.Stderr
    if err := cmd1.Start(); err != nil {
        log.Fatalf("Failed to start node 1: %v", err)
    }
    processes[1] = cmd1
    fmt.Println("Started node 1 on localhost:8001")

    cmd2 := exec.Command("./honeybadger",
        "-id", "2",
        "-addr", "localhost:8002",
        "-peers", peer2,
        "-n", "4",
        "-f", "1",
        "-batch", "64")
    cmd2.Stdout = os.Stdout
    cmd2.Stderr = os.Stderr
    if err := cmd2.Start(); err != nil {
        log.Fatalf("Failed to start node 2: %v", err)
    }
    processes[2] = cmd2
    fmt.Println("Started node 2 on localhost:8002")

    cmd3 := exec.Command("./honeybadger",
        "-id", "3",
        "-addr", "localhost:8003",
        "-peers", peer3,
        "-n", "4",
        "-f", "1",
        "-batch", "64")
    cmd3.Stdout = os.Stdout
    cmd3.Stderr = os.Stderr
    if err := cmd3.Start(); err != nil {
        log.Fatalf("Failed to start node 3: %v", err)
    }
    processes[3] = cmd3
    fmt.Println("Started node 3 on localhost:8003")

    // 运行60秒
    fmt.Println("Running for 60 seconds...")
    time.Sleep(60 * time.Second)

	// 停止所有节点
	fmt.Println("Stopping all nodes...")
	for i, cmd := range processes {
		if cmd.Process != nil {
			cmd.Process.Kill()
			fmt.Printf("Stopped node %d\n", i)
		}
	}

	fmt.Println("Network test completed!")
}

func registerGobTypes() {
	gob.Register(&hbbft.ACSMessage{})
	gob.Register(&hbbft.RBCMessage{})
	gob.Register(&hbbft.BBAMessage{})
	gob.Register(&hbbft.HBMessage{})
	gob.Register(&hbbft.BroadcastMessage{})
	gob.Register(&hbbft.AgreementMessage{})
	gob.Register(&hbbft.ProofRequest{})
	gob.Register(&hbbft.EchoRequest{})
	gob.Register(&hbbft.ReadyRequest{})
	gob.Register(&hbbft.BinaryValueRequest{})
	gob.Register(&hbbft.AuxRequest{})
	gob.Register(hbbft.SimpleTransaction(""))
	// 加密相关类型
	gob.Register(&hbbft.DecryptShareMsg{})
	gob.Register(&hbbft.EncryptedBatch{})
}

